
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言基础语法 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part5/" />
    
    
    <link rel="prev" href="../part3/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言基础语法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x57FA;&#x7840;&#x8BED;&#x6CD5;">Go &#x8BED;&#x8A00;&#x57FA;&#x7840;&#x8BED;&#x6CD5;</h1>
<p style="text-indent:2em">
&#x4E0A;&#x4E00;&#x7AE0;&#x8282;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4E86;&#x89E3;&#x4E86; Go &#x8BED;&#x8A00;&#x7684;&#x57FA;&#x672C;&#x7EC4;&#x6210;&#x7ED3;&#x6784;&#xFF0C;&#x672C;&#x7AE0;&#x8282;&#x6211;&#x4EEC;&#x5C06;&#x5B66;&#x4E60; Go &#x8BED;&#x8A00;&#x7684;&#x57FA;&#x7840;&#x8BED;&#x6CD5;&#x3002;
</p>

<hr>
<h2 id="go-&#x6807;&#x8BB0;">Go &#x6807;&#x8BB0;</h2>
<p>Go &#x7A0B;&#x5E8F;&#x53EF;&#x4EE5;&#x7531;&#x591A;&#x4E2A;&#x6807;&#x8BB0;&#x7EC4;&#x6210;&#xFF0C;&#x53EF;&#x4EE5;&#x662F;<code>&#x5173;&#x952E;&#x5B57;</code>&#xFF0C;<code>&#x6807;&#x8BC6;&#x7B26;</code>&#xFF0C;<code>&#x5E38;&#x91CF;</code>&#xFF0C;<code>&#x5B57;&#x7B26;&#x4E32;</code>&#xFF0C;<code>&#x7B26;&#x53F7;</code>&#x3002;&#x5982;&#x4EE5;&#x4E0B; GO &#x8BED;&#x53E5;&#x7531; 6 &#x4E2A;&#x6807;&#x8BB0;&#x7EC4;&#x6210;&#xFF1A;</p>
<pre><code class="lang-go">fmt.Println(<span class="hljs-string">&quot;Hello, World!&quot;</span>)
</code></pre>
<p>6 &#x4E2A;&#x6807;&#x8BB0;&#x662F;(&#x6BCF;&#x884C;&#x4E00;&#x4E2A;)&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-number">1.</span> fmt
<span class="hljs-number">2.</span> .
<span class="hljs-number">3.</span> Println
<span class="hljs-number">4.</span> (
<span class="hljs-number">5.</span> <span class="hljs-string">&quot;Hello, World!&quot;</span>
<span class="hljs-number">6.</span> )
</code></pre>
<hr>
<h2 id="&#x884C;&#x5206;&#x9694;&#x7B26;">&#x884C;&#x5206;&#x9694;&#x7B26;</h2>
<p style="text-indent:2em">
&#x5728; Go &#x7A0B;&#x5E8F;&#x4E2D;&#xFF0C;&#x4E00;&#x884C;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;&#x8BED;&#x53E5;&#x7ED3;&#x675F;&#x3002;&#x6BCF;&#x4E2A;&#x8BED;&#x53E5;&#x4E0D;&#x9700;&#x8981;&#x50CF; C &#x5BB6;&#x65CF;&#x4E2D;&#x7684;&#x5176;&#x5B83;&#x8BED;&#x8A00;&#x4E00;&#x6837;&#x4EE5;&#x5206;&#x53F7; ; &#x7ED3;&#x5C3E;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4E9B;&#x5DE5;&#x4F5C;&#x90FD;&#x5C06;&#x7531; Go &#x7F16;&#x8BD1;&#x5668;&#x81EA;&#x52A8;&#x5B8C;&#x6210;&#x3002;
</p>

<p style="text-indent:2em">
&#x5982;&#x679C;&#x4F60;&#x6253;&#x7B97;&#x5C06;&#x591A;&#x4E2A;&#x8BED;&#x53E5;&#x5199;&#x5728;&#x540C;&#x4E00;&#x884C;&#xFF0C;&#x5B83;&#x4EEC;&#x5219;&#x5FC5;&#x987B;&#x4F7F;&#x7528; ; &#x4EBA;&#x4E3A;&#x533A;&#x5206;&#xFF0C;&#x4F46;&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x9F13;&#x52B1;&#x8FD9;&#x79CD;&#x505A;&#x6CD5;&#x3002;
</p>

<p>&#x4EE5;&#x4E0B;&#x4E3A;&#x4E24;&#x4E2A;&#x8BED;&#x53E5;&#xFF1A;</p>
<pre><code class="lang-go">fmt.Println(<span class="hljs-string">&quot;Hello, World!&quot;</span>)
fmt.Println(<span class="hljs-string">&quot;Go Go Go!!!&quot;</span>)
</code></pre>
<hr>
<h2 id="&#x6CE8;&#x91CA;">&#x6CE8;&#x91CA;</h2>
<p style="text-indent:2em">
&#x6CE8;&#x91CA;&#x4E0D;&#x4F1A;&#x88AB;&#x7F16;&#x8BD1;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A;&#x5305;&#x5E94;&#x8BE5;&#x6709;&#x76F8;&#x5173;&#x6CE8;&#x91CA;&#x3002;
</p>

<p style="text-indent:2em">
&#x5355;&#x884C;&#x6CE8;&#x91CA;&#x662F;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x6CE8;&#x91CA;&#x5F62;&#x5F0F;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#x4EE5; // &#x5F00;&#x5934;&#x7684;&#x5355;&#x884C;&#x6CE8;&#x91CA;&#x3002;&#x591A;&#x884C;&#x6CE8;&#x91CA;&#x4E5F;&#x53EB;&#x5757;&#x6CE8;&#x91CA;&#xFF0C;&#x5747;&#x5DF2;&#x4EE5; /* &#x5F00;&#x5934;&#xFF0C;&#x5E76;&#x4EE5; */ &#x7ED3;&#x5C3E;&#x3002;&#x5982;&#xFF1A;
</p>

<pre><code class="lang-go"><span class="hljs-comment">// &#x5355;&#x884C;&#x6CE8;&#x91CA;</span>
<span class="hljs-comment">/*
&#x6211;&#x662F;&#x591A;&#x884C;&#x6CE8;&#x91CA;
*/</span>
</code></pre>
<hr>
<h2 id="&#x6807;&#x8BC6;&#x7B26;">&#x6807;&#x8BC6;&#x7B26;</h2>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x6807;&#x8BC6;&#x7B26;&#x7528;&#x6765;&#x547D;&#x540D;&#x53D8;&#x91CF;&#x3001;&#x7C7B;&#x578B;&#x7B49;&#x7A0B;&#x5E8F;&#x5B9E;&#x4F53;&#x3002;&#x4E00;&#x4E2A;&#x6807;&#x8BC6;&#x7B26;&#x5B9E;&#x9645;&#x4E0A;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x6216;&#x662F;&#x591A;&#x4E2A;<code>&#x5B57;&#x6BCD;(A~Z&#x548C;a~z)</code> <code>&#x6570;&#x5B57;(0~9)</code>&#x3001;<code>&#x4E0B;&#x5212;&#x7EBF;_</code>&#x7EC4;&#x6210;&#x7684;&#x5E8F;&#x5217;&#xFF0C;&#x4F46;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x5FC5;&#x987B;&#x662F;&#x5B57;&#x6BCD;&#x6216;&#x4E0B;&#x5212;&#x7EBF;&#x800C;&#x4E0D;&#x80FD;&#x662F;&#x6570;&#x5B57;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x662F;&#x6709;&#x6548;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#xFF1A;</p>
<pre><code>mahesh   kumar   abc   move_name   a_123
myname50   _temp   j   a23b9   retVal
</code></pre><p>&#x4EE5;&#x4E0B;&#x662F;&#x65E0;&#x6548;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#xFF1A;</p>
<ul>
<li>1ab&#xFF08;&#x4EE5;&#x6570;&#x5B57;&#x5F00;&#x5934;&#xFF09;</li>
<li>case&#xFF08;Go &#x8BED;&#x8A00;&#x7684;&#x5173;&#x952E;&#x5B57;&#xFF09;</li>
<li>a+b&#xFF08;&#x8FD0;&#x7B97;&#x7B26;&#x662F;&#x4E0D;&#x5141;&#x8BB8;&#x7684;&#xFF09;</li>
</ul>
<hr>
<h2 id="&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;">&#x5B57;&#x7B26;&#x4E32;&#x8FDE;&#x63A5;</h2>
<p>Go &#x8BED;&#x8A00;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <strong><code>+</code></strong> &#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main
<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>
<span class="hljs-keyword">func</span> main() {
    fmt.Println(<span class="hljs-string">&quot;Apple&quot;</span> + <span class="hljs-string">&quot;Pen&quot;</span>)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">ApplePen
</code></pre>
<h2 id="&#x5173;&#x952E;&#x5B57;">&#x5173;&#x952E;&#x5B57;</h2>
<p>&#x4E0B;&#x9762;&#x5217;&#x4E3E;&#x4E86; Go &#x4EE3;&#x7801;&#x4E2D;&#x4F1A;&#x4F7F;&#x7528;&#x5230;&#x7684; 25 &#x4E2A;&#x5173;&#x952E;&#x5B57;&#x6216;&#x4FDD;&#x7559;&#x5B57;&#xFF1A;</p>
<table>
    <tr>
        <td>break</td>
        <td>default</td>
        <td>func</td>
        <td>interface</td>
        <td>select</td>
    </tr>
    <tr>
        <td>case</td>
        <td>defer</td>
        <td>go</td>
        <td>map</td>
        <td>truct</td>
    </tr>
    <tr>
        <td>chan</td>
        <td>else</td>
        <td>goto</td>
        <td>package</td>
        <td>switch</td>
    </tr>
    <tr>
        <td>const</td>
        <td>fallthrough</td>
        <td>if</td>
        <td>range</td>
        <td>type</td>
    </tr>
    <tr>
        <td>continue</td>
        <td>for</td>
        <td>import</td>
        <td>return</td>
        <td>var</td>
    </tr>
</table>

<p>&#x9664;&#x4E86;&#x4EE5;&#x4E0A;&#x4ECB;&#x7ECD;&#x7684;&#x8FD9;&#x4E9B;&#x5173;&#x952E;&#x5B57;&#xFF0C;Go &#x8BED;&#x8A00;&#x8FD8;&#x6709; 36 &#x4E2A;&#x9884;&#x5B9A;&#x4E49;&#x6807;&#x8BC6;&#x7B26;&#xFF1A;</p>
<table>
    <tr>
        <td>append</td>
        <td>bool</td>
        <td>byte</td>
        <td>cap</td>
        <td>close</td>
        <td>complex</td>
        <td>complex64</td>
        <td>complex128</td>
        <td>uint16</td>
    </tr>
    <tr>
        <td>copy</td>
        <td>false</td>
        <td>float32</td>
        <td>float64</td>
        <td>imag</td>
        <td>int</td>
        <td>int8</td>
        <td>int16</td>
        <td>uint32</td>
    </tr>
    <tr>
        <td>int32</td>
        <td>int64</td>
        <td>iota</td>
        <td>len</td>
        <td>make</td>
        <td>new</td>
        <td>nil</td>
        <td>panic</td>
        <td>uint64</td>
    </tr>
    <tr>
        <td>print</td>
        <td>println</td>
        <td>real</td>
        <td>recover</td>
        <td>string</td>
        <td>true</td>
        <td>uint</td>
        <td>uint8</td>
        <td>uintptr</td>
    </tr>
</table>

<ul>
<li>&#x7A0B;&#x5E8F;&#x4E00;&#x822C;&#x7531;<code>&#x5173;&#x952E;&#x5B57;</code>&#x3001;<code>&#x5E38;&#x91CF;</code>&#x3001;<code>&#x53D8;&#x91CF;</code>&#x3001;<code>&#x8FD0;&#x7B97;&#x7B26;</code>&#x3001;<code>&#x7C7B;&#x578B;</code>&#x548C;<code>&#x51FD;&#x6570;</code>&#x7EC4;&#x6210;</li>
<li>&#x7A0B;&#x5E8F;&#x4E2D;&#x53EF;&#x80FD;&#x4F1A;&#x4F7F;&#x7528;&#x5230;&#x8FD9;&#x4E9B;&#x5206;&#x9694;&#x7B26;&#xFF1A;&#x62EC;&#x53F7; <code>()</code>&#xFF0C;&#x4E2D;&#x62EC;&#x53F7; <code>[]</code> &#x548C;&#x5927;&#x62EC;&#x53F7; <code>{}</code></li>
<li>&#x7A0B;&#x5E8F;&#x4E2D;&#x53EF;&#x80FD;&#x4F1A;&#x4F7F;&#x7528;&#x5230;&#x8FD9;&#x4E9B;&#x6807;&#x70B9;&#x7B26;&#x53F7;&#xFF1A;<code>.</code>&#x3001;<code>,</code>&#x3001;<code>;</code>&#x3001;<code>:</code> &#x548C; <code>&#x2026;</code></li>
</ul>
<hr>
<h2 id="go-&#x8BED;&#x8A00;&#x7684;&#x7A7A;&#x683C;">Go &#x8BED;&#x8A00;&#x7684;&#x7A7A;&#x683C;</h2>
<p>Go &#x8BED;&#x8A00;&#x4E2D;&#x53D8;&#x91CF;&#x7684;&#x58F0;&#x660E;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x7A7A;&#x683C;&#x9694;&#x5F00;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> age <span class="hljs-keyword">int</span>
</code></pre>
<p>&#x8BED;&#x53E5;&#x4E2D;&#x9002;&#x5F53;&#x4F7F;&#x7528;&#x7A7A;&#x683C;&#x80FD;&#x8BA9;&#x7A0B;&#x5E8F;&#x66F4;&#x6613;&#x9605;&#x8BFB;&#x3002;</p>
<p>&#x65E0;&#x7A7A;&#x683C;&#xFF1A;</p>
<pre><code class="lang-go">fruit=apples+oranges
</code></pre>
<p>&#x5728;&#x53D8;&#x91CF;&#x4E0E;&#x8FD0;&#x7B97;&#x7B26;&#x95F4;&#x52A0;&#x5165;&#x7A7A;&#x683C;&#xFF0C;&#x7A0B;&#x5E8F;&#x770B;&#x8D77;&#x6765;&#x66F4;&#x52A0;&#x7F8E;&#x89C2;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre><code class="lang-go">fruit = apples + oranges
</code></pre>
<hr>
<h2 id="sp">SP</h2>
<h3 id="golang-fmt-&#x5305;">Golang fmt &#x5305;</h3>
<p>Print() &#x51FD;&#x6570;&#x5C06;&#x53C2;&#x6570;&#x5217;&#x8868; a &#x4E2D;&#x7684;&#x5404;&#x4E2A;&#x53C2;&#x6570;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x5E76;&#x5199;&#x5165;&#x5230;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x4E2D;&#x3002;
&#x975E;&#x5B57;&#x7B26;&#x4E32;&#x53C2;&#x6570;&#x4E4B;&#x95F4;&#x4F1A;&#x6DFB;&#x52A0;&#x7A7A;&#x683C;&#xFF0C;&#x8FD4;&#x56DE;&#x5199;&#x5165;&#x7684;&#x5B57;&#x8282;&#x6570;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Print(a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p>Println() &#x51FD;&#x6570;&#x529F;&#x80FD;&#x7C7B;&#x4F3C; Print&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x6700;&#x540E;&#x4F1A;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x6362;&#x884C;&#x7B26;&#x3002;
&#x6240;&#x6709;&#x53C2;&#x6570;&#x4E4B;&#x95F4;&#x4F1A;&#x6DFB;&#x52A0;&#x7A7A;&#x683C;&#xFF0C;&#x8FD4;&#x56DE;&#x5199;&#x5165;&#x7684;&#x5B57;&#x8282;&#x6570;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Println(a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p>Printf() &#x51FD;&#x6570;&#x5C06;&#x53C2;&#x6570;&#x5217;&#x8868; a &#x586B;&#x5199;&#x5230;&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32; format &#x7684;&#x5360;&#x4F4D;&#x7B26;&#x4E2D;&#x3002;
&#x586B;&#x5199;&#x540E;&#x7684;&#x7ED3;&#x679C;&#x5199;&#x5165;&#x5230;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x4E2D;&#xFF0C;&#x8FD4;&#x56DE;&#x5199;&#x5165;&#x7684;&#x5B57;&#x8282;&#x6570;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Printf(format <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#x529F;&#x80FD;&#x540C;&#x4E0A;&#x9762;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x5C06;&#x8F6C;&#x6362;&#x7ED3;&#x679C;&#x5199;&#x5165;&#x5230; <strong><code>w</code></strong> &#x4E2D;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Fprint(w io.Writer, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
<span class="hljs-keyword">func</span> Fprintln(w io.Writer, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
<span class="hljs-keyword">func</span> Fprintf(w io.Writer, format <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#x529F;&#x80FD;&#x540C;&#x4E0A;&#x9762;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x5C06;&#x8F6C;&#x6362;&#x7ED3;&#x679C;&#x4EE5;&#x5B57;&#x7B26;&#x4E32;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Sprint(a ...<span class="hljs-keyword">interface</span>{}) <span class="hljs-keyword">string</span>
<span class="hljs-keyword">func</span> Sprintln(a ...<span class="hljs-keyword">interface</span>{}) <span class="hljs-keyword">string</span>
<span class="hljs-keyword">func</span> Sprintf(format <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) <span class="hljs-keyword">string</span>
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x51FD;&#x6570;&#x529F;&#x80FD;&#x540C; Sprintf() &#x51FD;&#x6570;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x7ED3;&#x679C;&#x5B57;&#x7B26;&#x4E32;&#x88AB;&#x5305;&#x88C5;&#x6210;&#x4E86; error &#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Errorf(format <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) error
</code></pre>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> main() {
    fmt.Print(<span class="hljs-string">&quot;a&quot;</span>, <span class="hljs-string">&quot;b&quot;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&quot;c&quot;</span>, <span class="hljs-string">&quot;d&quot;</span>, <span class="hljs-string">&quot;\n&quot;</span>)
    fmt.Println(<span class="hljs-string">&quot;a&quot;</span>, <span class="hljs-string">&quot;b&quot;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&quot;c&quot;</span>, <span class="hljs-string">&quot;d&quot;</span>)
    fmt.Printf(<span class="hljs-string">&quot;ab %d %d %d cd\n&quot;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
    <span class="hljs-comment">// ab1 2 3cd</span>
    <span class="hljs-comment">// a b 1 2 3 c d</span>
    <span class="hljs-comment">// ab 1 2 3 cd</span>

    <span class="hljs-keyword">if</span> err := percent(<span class="hljs-number">30</span>, <span class="hljs-number">70</span>, <span class="hljs-number">90</span>, <span class="hljs-number">160</span>); err != <span class="hljs-literal">nil</span> {
        fmt.Println(err)
    }
    <span class="hljs-comment">// 30%</span>
    <span class="hljs-comment">// 70%</span>
    <span class="hljs-comment">// 90%</span>
    <span class="hljs-comment">// &#x6570;&#x503C; 160 &#x8D85;&#x51FA;&#x8303;&#x56F4;&#xFF08;100&#xFF09;</span>
}

<span class="hljs-keyword">func</span> percent(i ...<span class="hljs-keyword">int</span>) error {
    <span class="hljs-keyword">for</span> _, n := <span class="hljs-keyword">range</span> i {
        <span class="hljs-keyword">if</span> n &gt; <span class="hljs-number">100</span> {
            <span class="hljs-keyword">return</span> fmt.Errorf(<span class="hljs-string">&quot;&#x6570;&#x503C; %d &#x8D85;&#x51FA;&#x8303;&#x56F4;&#xFF08;100&#xFF09;&quot;</span>, n)
        }
        fmt.Print(n, <span class="hljs-string">&quot;%\n&quot;</span>)
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}
</code></pre>
<p>Formatter &#x7531;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x5B9E;&#x73B0;&#xFF0C;&#x7528;&#x4E8E;&#x5B9E;&#x73B0;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x683C;&#x5F0F;&#x5316;&#x8FC7;&#x7A0B;&#x3002;
&#x5F53;&#x683C;&#x5F0F;&#x5316;&#x5668;&#x9700;&#x8981;&#x683C;&#x5F0F;&#x5316;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x5176; Format &#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Formatter <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// f &#x7528;&#x4E8E;&#x83B7;&#x53D6;&#x5360;&#x4F4D;&#x7B26;&#x7684;&#x65D7;&#x6807;&#x3001;&#x5BBD;&#x5EA6;&#x3001;&#x7CBE;&#x5EA6;&#x7B49;&#x4FE1;&#x606F;&#xFF0C;&#x4E5F;&#x7528;&#x4E8E;&#x8F93;&#x51FA;&#x683C;&#x5F0F;&#x5316;&#x7684;&#x7ED3;&#x679C;</span>
    <span class="hljs-comment">// c &#x662F;&#x5360;&#x4F4D;&#x7B26;&#x4E2D;&#x7684;&#x52A8;&#x8BCD;</span>
    Format(f State, c <span class="hljs-keyword">rune</span>)
}
</code></pre>
<p>&#x7531;&#x683C;&#x5F0F;&#x5316;&#x5668;&#xFF08;Print &#x4E4B;&#x7C7B;&#x7684;&#x51FD;&#x6570;&#xFF09;&#x5B9E;&#x73B0;&#xFF0C;&#x7528;&#x4E8E;&#x7ED9;&#x81EA;&#x5B9A;&#x4E49;&#x683C;&#x5F0F;&#x5316;&#x8FC7;&#x7A0B;&#x63D0;&#x4F9B;&#x4FE1;&#x606F;:</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> State <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// Formatter &#x901A;&#x8FC7; Write &#x65B9;&#x6CD5;&#x5C06;&#x683C;&#x5F0F;&#x5316;&#x7ED3;&#x679C;&#x5199;&#x5165;&#x683C;&#x5F0F;&#x5316;&#x5668;&#x4E2D;&#xFF0C;&#x4EE5;&#x4FBF;&#x8F93;&#x51FA;&#x3002;</span>
    Write(b []<span class="hljs-keyword">byte</span>) (ret <span class="hljs-keyword">int</span>, err error)
    <span class="hljs-comment">// Formatter &#x901A;&#x8FC7; Width &#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x5360;&#x4F4D;&#x7B26;&#x4E2D;&#x7684;&#x5BBD;&#x5EA6;&#x4FE1;&#x606F;&#x53CA;&#x5176;&#x662F;&#x5426;&#x88AB;&#x8BBE;&#x7F6E;&#x3002;</span>
    Width() (wid <span class="hljs-keyword">int</span>, ok <span class="hljs-keyword">bool</span>)
    <span class="hljs-comment">// Formatter &#x901A;&#x8FC7; Precision &#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x5360;&#x4F4D;&#x7B26;&#x4E2D;&#x7684;&#x7CBE;&#x5EA6;&#x4FE1;&#x606F;&#x53CA;&#x5176;&#x662F;&#x5426;&#x88AB;&#x8BBE;&#x7F6E;&#x3002;</span>
    Precision() (prec <span class="hljs-keyword">int</span>, ok <span class="hljs-keyword">bool</span>)
    <span class="hljs-comment">// Formatter &#x901A;&#x8FC7; Flag &#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x5360;&#x4F4D;&#x7B26;&#x4E2D;&#x7684;&#x65D7;&#x6807;[+- 0#]&#x662F;&#x5426;&#x88AB;&#x8BBE;&#x7F6E;&#x3002;</span>
    Flag(c <span class="hljs-keyword">int</span>) <span class="hljs-keyword">bool</span>
}
</code></pre>
<p>Stringer &#x7531;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x5B9E;&#x73B0;&#xFF0C;&#x7528;&#x4E8E;&#x5B9E;&#x73B0;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x683C;&#x5F0F;&#x5316;&#x8FC7;&#x7A0B;&#x3002;
&#x5F53;&#x683C;&#x5F0F;&#x5316;&#x5668;&#x9700;&#x8981;&#x8F93;&#x51FA;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#x65F6;&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x5176; String &#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Stringer <span class="hljs-keyword">interface</span> {
    String() <span class="hljs-keyword">string</span>
}
</code></pre>
<p>Stringer &#x7531;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x5B9E;&#x73B0;&#xFF0C;&#x7528;&#x4E8E;&#x5B9E;&#x73B0;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x683C;&#x5F0F;&#x5316;&#x8FC7;&#x7A0B;&#x3002;
&#x5F53;&#x683C;&#x5F0F;&#x5316;&#x5668;&#x9700;&#x8981;&#x8F93;&#x51FA;&#x8BE5;&#x7C7B;&#x578B;&#x7684; Go &#x8BED;&#x6CD5;&#x5B57;&#x7B26;&#x4E32;&#xFF08;%#v&#xFF09;&#x65F6;&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x5176; String &#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> GoStringer <span class="hljs-keyword">interface</span> {
    GoString() <span class="hljs-keyword">string</span>
}
</code></pre>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Ustr <span class="hljs-keyword">string</span>

<span class="hljs-keyword">func</span> (us Ustr) String() <span class="hljs-keyword">string</span> {
    <span class="hljs-keyword">return</span> strings.ToUpper(<span class="hljs-keyword">string</span>(us))
}

<span class="hljs-keyword">func</span> (us Ustr) GoString() <span class="hljs-keyword">string</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-string">`&quot;`</span> + strings.ToUpper(<span class="hljs-keyword">string</span>(us)) + <span class="hljs-string">`&quot;`</span>
}

<span class="hljs-keyword">func</span> (u Ustr) Format(f fmt.State, c <span class="hljs-keyword">rune</span>) {
    write := <span class="hljs-keyword">func</span>(s <span class="hljs-keyword">string</span>) {
        f.Write([]<span class="hljs-keyword">byte</span>(s))
    }
    <span class="hljs-keyword">switch</span> c {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;m&apos;</span>, <span class="hljs-string">&apos;M&apos;</span>:
        write(<span class="hljs-string">&quot;&#x65D7;&#x6807;&#xFF1A;[&quot;</span>)
        <span class="hljs-keyword">for</span> s := <span class="hljs-string">&quot;+- 0#&quot;</span>; <span class="hljs-built_in">len</span>(s) &gt; <span class="hljs-number">0</span>; s = s[<span class="hljs-number">1</span>:] {
            <span class="hljs-keyword">if</span> f.Flag(<span class="hljs-keyword">int</span>(s[<span class="hljs-number">0</span>])) {
                write(s[:<span class="hljs-number">1</span>])
            }
        }
        write(<span class="hljs-string">&quot;]&quot;</span>)
        <span class="hljs-keyword">if</span> v, ok := f.Width(); ok {
            write(<span class="hljs-string">&quot; | &#x5BBD;&#x5EA6;&#xFF1A;&quot;</span> + strconv.FormatInt(<span class="hljs-keyword">int64</span>(v), <span class="hljs-number">10</span>))
        }
        <span class="hljs-keyword">if</span> v, ok := f.Precision(); ok {
            write(<span class="hljs-string">&quot; | &#x7CBE;&#x5EA6;&#xFF1A;&quot;</span> + strconv.FormatInt(<span class="hljs-keyword">int64</span>(v), <span class="hljs-number">10</span>))
        }
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;s&apos;</span>, <span class="hljs-string">&apos;v&apos;</span>: <span class="hljs-comment">// &#x5982;&#x679C;&#x4F7F;&#x7528; Format &#x51FD;&#x6570;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x81EA;&#x5DF1;&#x5904;&#x7406;&#x6240;&#x6709;&#x683C;&#x5F0F;&#xFF0C;&#x5305;&#x62EC; %#v</span>
        <span class="hljs-keyword">if</span> c == <span class="hljs-string">&apos;v&apos;</span> &amp;&amp; f.Flag(<span class="hljs-string">&apos;#&apos;</span>) {
            write(u.GoString())
        } <span class="hljs-keyword">else</span> {
            write(u.String())
        }
    <span class="hljs-keyword">default</span>: <span class="hljs-comment">// &#x5982;&#x679C;&#x4F7F;&#x7528; Format &#x51FD;&#x6570;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x81EA;&#x5DF1;&#x5904;&#x7406;&#x9ED8;&#x8BA4;&#x8F93;&#x51FA;</span>
        write(<span class="hljs-string">&quot;&#x65E0;&#x6548;&#x683C;&#x5F0F;&#xFF1A;&quot;</span> + <span class="hljs-keyword">string</span>(c))
    }
}

<span class="hljs-keyword">func</span> main() {
    u := Ustr(<span class="hljs-string">&quot;Hello World!&quot;</span>)
    <span class="hljs-comment">// &quot;-&quot; &#x6807;&#x8BB0;&#x548C; &quot;0&quot; &#x6807;&#x8BB0;&#x4E0D;&#x80FD;&#x540C;&#x65F6;&#x5B58;&#x5728;</span>
    fmt.Printf(<span class="hljs-string">&quot;%-+ 0#8.5m\n&quot;</span>, u) <span class="hljs-comment">// &#x65D7;&#x6807;&#xFF1A;[+- #] | &#x5BBD;&#x5EA6;&#xFF1A;8 | &#x7CBE;&#x5EA6;&#xFF1A;5</span>
    fmt.Printf(<span class="hljs-string">&quot;%+ 0#8.5M\n&quot;</span>, u)  <span class="hljs-comment">// &#x65D7;&#x6807;&#xFF1A;[+ 0#] | &#x5BBD;&#x5EA6;&#xFF1A;8 | &#x7CBE;&#x5EA6;&#xFF1A;5</span>
    fmt.Println(u)                <span class="hljs-comment">// HELLO WORLD!</span>
    fmt.Printf(<span class="hljs-string">&quot;%s\n&quot;</span>, u)         <span class="hljs-comment">// HELLO WORLD!</span>
    fmt.Printf(<span class="hljs-string">&quot;%#v\n&quot;</span>, u)        <span class="hljs-comment">// &quot;HELLO WORLD!&quot;</span>
    fmt.Printf(<span class="hljs-string">&quot;%d\n&quot;</span>, u)         <span class="hljs-comment">// &#x65E0;&#x6548;&#x683C;&#x5F0F;&#xFF1A;d</span>
}
</code></pre>
<p><strong><code>Scan</code></strong> &#x4ECE;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x4E2D;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x5C06;&#x6570;&#x636E;&#x7528;&#x7A7A;&#x767D;&#x5206;&#x5272;&#x5E76;&#x89E3;&#x6790;&#x540E;&#x5B58;&#x5165; a &#x63D0;&#x4F9B;&#x7684;&#x53D8;&#x91CF;&#x4E2D;&#xFF08;&#x6362;&#x884C;&#x7B26;&#x4F1A;&#x88AB;&#x5F53;&#x4F5C;&#x7A7A;&#x767D;&#x5904;&#x7406;&#xFF09;&#xFF0C;&#x53D8;&#x91CF;&#x5FC5;&#x987B;&#x4EE5;&#x6307;&#x9488;&#x4F20;&#x5165;&#x3002;
&#x5F53;&#x8BFB;&#x5230; EOF &#x6216;&#x6240;&#x6709;&#x53D8;&#x91CF;&#x90FD;&#x586B;&#x5199;&#x5B8C;&#x6BD5;&#x5219;&#x505C;&#x6B62;&#x626B;&#x63CF;&#x3002;
&#x8FD4;&#x56DE;&#x6210;&#x529F;&#x89E3;&#x6790;&#x7684;&#x53C2;&#x6570;&#x6570;&#x91CF;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Scan(a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p><strong><code>Scanln</code></strong> &#x548C; <strong><code>Scan</code></strong> &#x7C7B;&#x4F3C;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x9047;&#x5230;&#x6362;&#x884C;&#x7B26;&#x5C31;&#x505C;&#x6B62;&#x626B;&#x63CF;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Scanln(a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p><code>Scanf</code> &#x4ECE;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x4E2D;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x6839;&#x636E;&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32; <code>format</code> &#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x89E3;&#x6790;&#xFF0C;&#x5C06;&#x89E3;&#x6790;&#x7ED3;&#x679C;&#x5B58;&#x5165;&#x53C2;&#x6570; <code>a</code> &#x6240;&#x63D0;&#x4F9B;&#x7684;&#x53D8;&#x91CF;&#x4E2D;&#xFF0C;&#x53D8;&#x91CF;&#x5FC5;&#x987B;&#x4EE5;&#x6307;&#x9488;&#x4F20;&#x5165;&#x3002;
&#x8F93;&#x5165;&#x7AEF;&#x7684;&#x6362;&#x884C;&#x7B26;&#x5FC5;&#x987B;&#x548C; <code>format</code> &#x4E2D;&#x7684;&#x6362;&#x884C;&#x7B26;&#x76F8;&#x5BF9;&#x5E94;&#xFF08;&#x5982;&#x679C;&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x6709;&#x6362;&#x884C;&#x7B26;&#xFF0C;&#x5219;&#x8F93;&#x5165;&#x7AEF;&#x5FC5;&#x987B;&#x8F93;&#x5165;&#x76F8;&#x5E94;&#x7684;&#x6362;&#x884C;&#x7B26;&#xFF09;&#x3002;
&#x5360;&#x4F4D;&#x7B26; <code>%c</code> &#x603B;&#x662F;&#x5339;&#x914D;&#x4E0B;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x5305;&#x62EC;&#x7A7A;&#x767D;&#xFF0C;&#x6BD4;&#x5982;&#x7A7A;&#x683C;&#x7B26;&#x3001;&#x5236;&#x8868;&#x7B26;&#x3001;&#x6362;&#x884C;&#x7B26;&#x3002;
&#x8FD4;&#x56DE;&#x6210;&#x529F;&#x89E3;&#x6790;&#x7684;&#x53C2;&#x6570;&#x6570;&#x91CF;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Scanf(format <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#x529F;&#x80FD;&#x540C;&#x4E0A;&#x9762;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x4ECE; r &#x4E2D;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Fscan(r io.Reader, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
<span class="hljs-keyword">func</span> Fscanln(r io.Reader, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
<span class="hljs-keyword">func</span> Fscanf(r io.Reader, format <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#x529F;&#x80FD;&#x540C;&#x4E0A;&#x9762;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x4ECE; str &#x4E2D;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Sscan(str <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
<span class="hljs-keyword">func</span> Sscanln(str <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
<span class="hljs-keyword">func</span> Sscanf(str <span class="hljs-keyword">string</span>, format <span class="hljs-keyword">string</span>, a ...<span class="hljs-keyword">interface</span>{}) (n <span class="hljs-keyword">int</span>, err error)
</code></pre>
<p>&#x5B9E;&#x4F8B;:</p>
<pre><code class="lang-go"><span class="hljs-comment">// &#x5BF9;&#x4E8E; Scan &#x800C;&#x8A00;&#xFF0C;&#x56DE;&#x8F66;&#x89C6;&#x4E3A;&#x7A7A;&#x767D;</span>
<span class="hljs-keyword">func</span> main() {
    a, b, c := <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">false</span>
    fmt.Scan(&amp;a, &amp;b, &amp;c)
    fmt.Println(a, b, c)
    <span class="hljs-comment">// &#x5728;&#x7EC8;&#x7AEF;&#x6267;&#x884C;&#x540E;&#xFF0C;&#x8F93;&#x5165; abc 1 &#x56DE;&#x8F66; true &#x56DE;&#x8F66;</span>
    <span class="hljs-comment">// &#x7ED3;&#x679C; abc 1 true</span>
}

<span class="hljs-comment">// &#x5BF9;&#x4E8E; Scanln &#x800C;&#x8A00;&#xFF0C;&#x56DE;&#x8F66;&#x7ED3;&#x675F;&#x626B;&#x63CF;</span>
<span class="hljs-keyword">func</span> main() {
    a, b, c := <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">false</span>
    fmt.Scanln(&amp;a, &amp;b, &amp;c)
    fmt.Println(a, b, c)
    <span class="hljs-comment">// &#x5728;&#x7EC8;&#x7AEF;&#x6267;&#x884C;&#x540E;&#xFF0C;&#x8F93;&#x5165; abc 1 true &#x56DE;&#x8F66;</span>
    <span class="hljs-comment">// &#x7ED3;&#x679C; abc 1 true</span>
}

<span class="hljs-comment">// &#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x5BBD;&#x5EA6;</span>
<span class="hljs-keyword">func</span> main() {
    a, b, c := <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">false</span>
    fmt.Scanf(<span class="hljs-string">&quot;%4s%d%t&quot;</span>, &amp;a, &amp;b, &amp;c)
    fmt.Println(a, b, c)
    <span class="hljs-comment">// &#x5728;&#x7EC8;&#x7AEF;&#x6267;&#x884C;&#x540E;&#xFF0C;&#x8F93;&#x5165; 1234567true &#x56DE;&#x8F66;</span>
    <span class="hljs-comment">// &#x7ED3;&#x679C; 1234 567 true</span>
}
</code></pre>
<p><strong><code>Scanner</code></strong> &#x7531;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x5B9E;&#x73B0;&#xFF0C;&#x7528;&#x4E8E;&#x5B9E;&#x73B0;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x626B;&#x63CF;&#x8FC7;&#x7A0B;&#x3002;
&#x5F53;&#x626B;&#x63CF;&#x5668;&#x9700;&#x8981;&#x89E3;&#x6790;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x5176; <code>Scan</code> &#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Scanner <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// state &#x7528;&#x4E8E;&#x83B7;&#x53D6;&#x5360;&#x4F4D;&#x7B26;&#x4E2D;&#x7684;&#x5BBD;&#x5EA6;&#x4FE1;&#x606F;&#xFF0C;&#x4E5F;&#x7528;&#x4E8E;&#x4ECE;&#x626B;&#x63CF;&#x5668;&#x4E2D;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x89E3;&#x6790;&#x3002;</span>
    <span class="hljs-comment">// verb &#x662F;&#x5360;&#x4F4D;&#x7B26;&#x4E2D;&#x7684;&#x52A8;&#x8BCD;</span>
    Scan(state ScanState, verb <span class="hljs-keyword">rune</span>) error
}
</code></pre>
<p>&#x7531;&#x626B;&#x63CF;&#x5668;&#xFF08;Scan &#x4E4B;&#x7C7B;&#x7684;&#x51FD;&#x6570;&#xFF09;&#x5B9E;&#x73B0;&#xFF0C;&#x7528;&#x4E8E;&#x7ED9;&#x81EA;&#x5B9A;&#x4E49;&#x626B;&#x63CF;&#x8FC7;&#x7A0B;&#x63D0;&#x4F9B;&#x6570;&#x636E;&#x548C;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> ScanState <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// ReadRune &#x4ECE;&#x626B;&#x63CF;&#x5668;&#x4E2D;&#x8BFB;&#x53D6;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x5982;&#x679C;&#x7528;&#x5728; Scanln &#x7C7B;&#x7684;&#x626B;&#x63CF;&#x5668;&#x4E2D;&#xFF0C;</span>
    <span class="hljs-comment">// &#x5219;&#x8BE5;&#x65B9;&#x6CD5;&#x4F1A;&#x5728;&#x8BFB;&#x5230;&#x7B2C;&#x4E00;&#x4E2A;&#x6362;&#x884C;&#x7B26;&#x4E4B;&#x540E;&#x6216;&#x8BFB;&#x5230;&#x6307;&#x5B9A;&#x5BBD;&#x5EA6;&#x4E4B;&#x540E;&#x8FD4;&#x56DE; EOF&#x3002;</span>
    <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x201C;&#x8BFB;&#x53D6;&#x7684;&#x5B57;&#x7B26;&#x201D;&#x548C;&#x201C;&#x5B57;&#x7B26;&#x7F16;&#x7801;&#x6240;&#x5360;&#x7528;&#x7684;&#x5B57;&#x8282;&#x6570;&#x201D;</span>
    ReadRune() (r <span class="hljs-keyword">rune</span>, size <span class="hljs-keyword">int</span>, err error)
    <span class="hljs-comment">// UnreadRune &#x64A4;&#x6D88;&#x6700;&#x540E;&#x4E00;&#x6B21;&#x7684; ReadRune &#x64CD;&#x4F5C;&#xFF0C;</span>
    <span class="hljs-comment">// &#x4F7F;&#x4E0B;&#x6B21;&#x7684; ReadRune &#x64CD;&#x4F5C;&#x5F97;&#x5230;&#x4E0E;&#x524D;&#x4E00;&#x6B21; ReadRune &#x76F8;&#x540C;&#x7684;&#x7ED3;&#x679C;&#x3002;</span>
    UnreadRune() error
    <span class="hljs-comment">// SkipSpace &#x4E3A; Scan &#x65B9;&#x6CD5;&#x63D0;&#x4F9B;&#x8DF3;&#x8FC7;&#x5F00;&#x5934;&#x7A7A;&#x767D;&#x7684;&#x80FD;&#x529B;&#x3002;</span>
    <span class="hljs-comment">// &#x6839;&#x636E;&#x626B;&#x63CF;&#x5668;&#x7684;&#x4E0D;&#x540C;&#xFF08;Scan &#x6216; Scanln&#xFF09;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x8DF3;&#x8FC7;&#x6362;&#x884C;&#x7B26;&#x3002;</span>
    SkipSpace()
    <span class="hljs-comment">// Token &#x7528;&#x4E8E;&#x4ECE;&#x626B;&#x63CF;&#x5668;&#x4E2D;&#x8BFB;&#x53D6;&#x7B26;&#x5408;&#x8981;&#x6C42;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;</span>
    <span class="hljs-comment">// Token &#x4ECE;&#x626B;&#x63CF;&#x5668;&#x4E2D;&#x8BFB;&#x53D6;&#x8FDE;&#x7EED;&#x7684;&#x7B26;&#x5408; f(c) &#x7684;&#x5B57;&#x7B26; c&#xFF0C;&#x51C6;&#x5907;&#x89E3;&#x6790;&#x3002;</span>
    <span class="hljs-comment">// &#x5982;&#x679C; f &#x4E3A; nil&#xFF0C;&#x5219;&#x4F7F;&#x7528; !unicode.IsSpace(c) &#x4EE3;&#x66FF; f(c)&#x3002;</span>
    <span class="hljs-comment">// skipSpace&#xFF1A;&#x662F;&#x5426;&#x8DF3;&#x8FC7;&#x5F00;&#x5934;&#x7684;&#x8FDE;&#x7EED;&#x7A7A;&#x767D;&#x3002;&#x8FD4;&#x56DE;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x6570;&#x636E;&#x3002;</span>
    <span class="hljs-comment">// &#x6CE8;&#x610F;&#xFF1A;token &#x6307;&#x5411;&#x5171;&#x4EAB;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4E0B;&#x6B21;&#x7684; Token &#x64CD;&#x4F5C;&#x53EF;&#x80FD;&#x4F1A;&#x8986;&#x76D6;&#x672C;&#x6B21;&#x7684;&#x7ED3;&#x679C;&#x3002;</span>
    Token(skipSpace <span class="hljs-keyword">bool</span>, f <span class="hljs-keyword">func</span>(<span class="hljs-keyword">rune</span>) <span class="hljs-keyword">bool</span>) (token []<span class="hljs-keyword">byte</span>, err error)
    <span class="hljs-comment">// Width &#x8FD4;&#x56DE;&#x5360;&#x4F4D;&#x7B26;&#x4E2D;&#x7684;&#x5BBD;&#x5EA6;&#x503C;&#x4EE5;&#x53CA;&#x5BBD;&#x5EA6;&#x503C;&#x662F;&#x5426;&#x88AB;&#x8BBE;&#x7F6E;</span>
    Width() (wid <span class="hljs-keyword">int</span>, ok <span class="hljs-keyword">bool</span>)
    <span class="hljs-comment">// &#x56E0;&#x4E3A;&#x4E0A;&#x9762;&#x5B9E;&#x73B0;&#x4E86; ReadRune &#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5; Read &#x65B9;&#x6CD5;&#x6C38;&#x8FDC;&#x4E0D;&#x5E94;&#x8BE5;&#x88AB;&#x8C03;&#x7528;&#x3002;</span>
    <span class="hljs-comment">// &#x4E00;&#x4E2A;&#x597D;&#x7684; ScanState &#x5E94;&#x8BE5;&#x8BA9; Read &#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x76F8;&#x5E94;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x3002;</span>
    Read(buf []<span class="hljs-keyword">byte</span>) (n <span class="hljs-keyword">int</span>, err error)
}
</code></pre>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Ustr <span class="hljs-keyword">string</span>

<span class="hljs-keyword">func</span> (u *Ustr) Scan(state fmt.ScanState, verb <span class="hljs-keyword">rune</span>) (err error) {
    <span class="hljs-keyword">var</span> s []<span class="hljs-keyword">byte</span>
    <span class="hljs-keyword">switch</span> verb {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;S&apos;</span>:
        s, err = state.Token(<span class="hljs-literal">true</span>, <span class="hljs-keyword">func</span>(c <span class="hljs-keyword">rune</span>) <span class="hljs-keyword">bool</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;A&apos;</span> &lt;= c &amp;&amp; c &lt;= <span class="hljs-string">&apos;Z&apos;</span> })
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
            <span class="hljs-keyword">return</span>
        }
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;s&apos;</span>, <span class="hljs-string">&apos;v&apos;</span>:
        s, err = state.Token(<span class="hljs-literal">true</span>, <span class="hljs-keyword">func</span>(c <span class="hljs-keyword">rune</span>) <span class="hljs-keyword">bool</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;a&apos;</span> &lt;= c &amp;&amp; c &lt;= <span class="hljs-string">&apos;z&apos;</span> })
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
            <span class="hljs-keyword">return</span>
        }
    <span class="hljs-keyword">default</span>:
        <span class="hljs-keyword">return</span> fmt.Errorf(<span class="hljs-string">&quot;&#x65E0;&#x6548;&#x683C;&#x5F0F;&#xFF1A;%c&quot;</span>, verb)
    }
    *u = Ustr(s)
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> a, b, c, d, e Ustr
    n, err := fmt.Scanf(<span class="hljs-string">&quot;%3S%S%3s%2v%x&quot;</span>, &amp;a, &amp;b, &amp;c, &amp;d, &amp;e)
    fmt.Println(a, b, c, d, e)
    fmt.Println(n, err)
    <span class="hljs-comment">// &#x5728;&#x7EC8;&#x7AEF;&#x6267;&#x884C;&#x540E;&#xFF0C;&#x8F93;&#x5165; ABCDEFGabcdefg &#x56DE;&#x8F66;</span>
    <span class="hljs-comment">// &#x7ED3;&#x679C;&#xFF1A;</span>
    <span class="hljs-comment">// ABC DEFG abc de</span>
    <span class="hljs-comment">// 4 &#x65E0;&#x6548;&#x683C;&#x5F0F;&#xFF1A;x</span>
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part3/" class="navigation navigation-prev " aria-label="Previous page: Go 语言结构">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part5/" class="navigation navigation-next " aria-label="Next page: Go 语言数据类型">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言基础语法","level":"1.5","depth":1,"next":{"title":"Go 语言数据类型","level":"1.6","depth":1,"path":"part5/README.md","ref":"part5/README.md","articles":[]},"previous":{"title":"Go 语言结构","level":"1.4","depth":1,"path":"part3/README.md","ref":"part3/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part4/README.md","mtime":"2021-01-18T03:36:43.939Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

